package com.blkkj.huan.Controller;


import com.aliyuncs.iot.model.v20170420.RegistDeviceRequest;
import com.aliyuncs.iot.model.v20170420.RegistDeviceResponse;
import com.blkkj.huan.util.BaseTest;
import com.blkkj.huan.util.LogUtil;
import com.blkkj.huan.util.ServiceUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class registDeviceController extends BaseTest {
    @PostMapping("/registDevice")
    public String registDevice(String productKey, String deviceName){
//        List<String> deviceNames = ServiceUtil.deviceNameGenerator(10);
//        String deviceName = "huan";

        //注册单个设备
        registDeviceTest(productKey,deviceName);
        return "huan";
    }

    /**
     * 注册设备
     *
     * @param productKey 产品pk
     * @param deviceName 设备名称 非必须
     * @return 设备名称
     */
    public static String registDeviceTest(String productKey, String deviceName) {
        RegistDeviceRequest request = new RegistDeviceRequest();
        request.setProductKey(productKey);
        request.setDeviceName(deviceName);
        RegistDeviceResponse response = (RegistDeviceResponse)executeTest(request);
        if (response != null && response.getSuccess() != false) {
            LogUtil.print(
                    "创建设备成功！deviceName:" + response.getDeviceName() + ",deviceSecret:" + response.getDeviceSecret());
            return response.getDeviceName();
        } else {
            LogUtil.error("创建设备失败！requestId:" + response.getRequestId() + "原因：" + response.getErrorMessage());
        }
        return null;
    }

}
